<body>
    <h2>咨询管理</h2>
    <div style="margin:10px 0;"></div>
    <table id="list"></table>
    <div id="tb" style="padding:5px;height:auto">
        <div style="margin-bottom:5px;vertical-align:middle">
            <a href="#" onclick="javascript:consultListJs.newConsult()" class="easyui-linkbutton" iconCls="icon-add" plain="true">增加</a>
            <a href="#" onclick="javascript:consultListJs.delete()" class="easyui-linkbutton" iconCls="icon-edit" plain="true">删除</a>
            <a href="#" class="easyui-linkbutton"  plain="true">过滤条件：</a>
         	<a href="#" onclick="javascript:consultListJs.openDateFilter()" class="easyui-linkbutton"  iconCls="icon-time" plain="true">时间</a>
    		
            <a href="#" id="employeeButton">所有人</a>
                <div id="mm3" class="menu-content" style="background:#f0f0f0;padding:10px;text-align:middle;width:200px;height:auto;display:none">
    				<div class="ftitle">按照所有人搜索</div>
    			    <div id="employee_list">
            		</div> 
    			</div>
    		<a href="#" id="itemButton">整形项目</a>
    		<div id="mm2" class="menu-content" style="background:#f0f0f0;padding:10px;text-align:middle;width:200px;height:auto;display:none">
    				<div class="ftitle">指定整形项目过滤</div>
            		<div id="items_list" class="fitem">
            		</div> 
    		</div>
        </div>
        <div id="consult_date" style="padding:3px">
        	从:<input id="consult_from" type="text"></input>
        	到:<input id="consult_to" type="text"></input>
        	<span class="toolbar_pic"><a href="javascript:void(0)" class="easyui-linkbutton" onclick="javascript:consultListJs.searchByTime()" >搜索</a></span>
        </div>
        <div id="searchFilter">
        </div>
    </div>
    <div id="ConsultAction" class="easyui-dialog" style="width:600px;height:500px;padding:10px 20px;"
            closed="true">
    </div>
    <div id="ConsultDetail" title="咨询详细信息" class="easyui-dialog" closed="true" style="width:850px;height:550px">
    </div>
    <script type="text/javascript">
    
(function(consultListJs, $, undefined ) {    
	var countPerPage = 5;
	var start = 0;
	var zxmr_items;
	consultListJs.opendiag = function(url,a){
		$('#CustomerAction').dialog("open");
		$('#CustomerAction').dialog('refresh',url);
		
	}
	
	$('#list').datagrid({
		title:'咨询管理',
		iconCls:'icon-consult',
		height:300,
		fitColumns:true,
		singleSelect:true,
		url:null,
		rownumbers:true,
	    columns:[[
					{title:'标题',field:'title',width:80,sortable:true},
	             	{title:'客户信息',field:'customerName',width:80,sortable:true},
	             	{title:'商业机会',field:'opportunityName',width:80,sortable:true},
	             	{title:'添加人',field:'addUser',width:80,sortable:true},
	             	{title:'添加时间',field:'createTime',width:80,sortable:true},
	             ]],
		pagination:true,
        pageSize:countPerPage,
        pageList:[5,10,15,20,25],
        toolbar:'#tb',
	    onLoadSuccess: function(){   
	        $('#list').datagrid('loaded');
	    },

	    onDblClickCell: openConsult
	});
	$('#list').datagrid('getPager').pagination({   
	    displayMsg:'当前显示从 [{from}] 到 [{to}] 共[{total}]条记录',   
	    onSelectPage : function(pPageIndex, pPageSize) {   
	    	
	        var gridOpts = $('#list').datagrid('options');   
	        gridOpts.pageNumber = pPageIndex;   
	        gridOpts.pageSize = pPageSize;     

	        start = (pPageIndex -1)*pPageSize;
	        workbenchJs.loadData("dispatch/customer/opportunity/consult/all?start="+start+'&size='+countPerPage,consultListJs.loadConsultData,"查询咨询信息");
	    },
	    onChangePageSize : function(pageSize){
	    	countPerPage = pageSize;
	    }
	}); 
	consultListJs.init = function(){
		$('#consult_from').datebox();
		$('#consult_to').datebox();
		refresh();
	};
	consultListJs.newConsult = function(){
		workbenchJs.open1('component/customer/addConsult.html',this);
	};
	consultListJs.delete = function(){
        var row = $('#list').datagrid('getSelected');
        if (row){
        	workbenchJs.loadData("dispatch/customer/consult/delete?id="+row.id,consultListJs.deleteConsultResult,"删除咨询");
        }else
        	{
        		$.messager.alert("Error","请选择一条记录");
        	}
	};
	consultListJs.deleteConsultResult = function(result) {
		refresh();
	}
	consultListJs.loadConsultData = function(result){
    	var res = result.result;
        if(res == "SUCCESS")
        {
        	var payload = jQuery.parseJSON(result.payload.value);
        	$('#list').datagrid('loadData',payload);
        }else if(res == "COMPONENT_ERROR")
        {
        	$.messager.alert("Error",result.payload.value);
        }else
        {
        	$.messager.alert("Error",result.result);
        }
        $('#list').datagrid('loaded');
	};
	
	function openConsult(){
        var row = $('#list').datagrid('getSelected');
        if (row){
    		$('#ConsultDetail').dialog("open").dialog('setTitle','咨询详细信息');
    		$('#ConsultDetail').dialog('refresh','component/customer/viewConsult.jsp?id='+row.id);
        }else
        	{
        		$.messager.alert("Error","请选择一条记录");
        	}
	}

    function refresh(){
		$('#consult_date').hide();
		workbenchJs.loadData("dispatch/customer/opportunity/consult/all?start="+start+"&size="+countPerPage,consultListJs.loadConsultData,"查询咨询信息");
		workbenchJs.loadData("dispatch/manage/item/all",consultListJs.loadItemsResult,"查询整形项目");
		workbenchJs.loadData("dispatch/manage/employee/simpleAll",consultListJs.loadEmployeeResult,"查询用户列表");
    }
    
    consultListJs.loadItemsResult = function(result){		
    	if(result.result == "SUCCESS")
    	{
    		zxmr_items = jQuery.parseJSON(result.payload.value);
    		var html = "";
    		$(zxmr_items).each(function(index){
    			html+='<p><a href="#" onclick="javascript:consultListJs.searchByItems(\''+zxmr_items[index].name+'\')"> '+zxmr_items[index].name+' </a></p>';
    		});
    		$('#items_list').html(html);
    		
    		$('#itemButton').menubutton({
    		    menu: '#mm2'
    		    });
    	}

	};
	consultListJs.loadEmployeeResult = function(result){
    	if(result.result == "SUCCESS")
    	{
    		var rows = jQuery.parseJSON(result.payload.value);
    		var html = "";
    		html+='<p><a href="#" onclick="javascript:consultListJs.searchByEmployee(\'*\')"> 全部</a></p>';
    		$(rows).each(function(index){
    			html+='<p><a href="#" onclick="javascript:consultListJs.searchByEmployee(\''+rows[index].name+'\')"> '+rows[index].name+' </a></p>';
    		});
    		$('#employee_list').html(html);
    		$('#employeeButton').menubutton({
    		    menu: '#mm3'
    		    });
    	}
    };
	consultListJs.clearSearch = function()
	{
		countPerPage = 5;
		start = 0;
	    consult_items_search = '';
	    consult_employee_search = '';
		$('#consult_from').datebox('setValue','');
		$('#consult_to').datebox('setValue','');
		refreshSearchFilter();
		refresh();
	}
	
	consultListJs.openDateFilter = function(){
		$('#consult_date').show();
	}
	
    var consult_items_search = '';
    var consult_employee_search = '';
    consultListJs.searchByItems = function(item){
		consult_items_search = item;
		$('#list').datagrid("loading");
		refreshSearchFilter();
		start = 0;
		postSearch();
	}
	consultListJs.searchByEmployee = function(employee){
		consult_employee_search = employee;
		$('#list').datagrid("loading");
		refreshSearchFilter();
		start = 0;
		postSearch();
	}

	consultListJs.searchByTime = function(){
		$('#list').datagrid("loading");
		refreshSearchFilter();
		start = 0;
		postSearch();
	}
	function getSearchFilter()
	{
		var html = "查询条件:";
		if(consult_items_search != "" && typeof(consult_items_search) != 'undefined')
			html += " + "+consult_items_search;
		if(consult_employee_search != "" && typeof(consult_employee_search) != 'undefined')
			html += " + "+consult_employee_search;
		var consult_from = $('#consult_from').datebox('getValue');
		if(consult_from != "" && typeof(consult_from) != 'undefined')
			html += " + 时间从 "+consult_from;
		
		var consult_to = $('#consult_to').datebox('getValue');
		if(consult_to != "" && typeof(consult_to) != 'undefined')
			html += " + 时间到 "+consult_to;
		
		return html;
	}
	function refreshSearchFilter()
	{

		var html = getSearchFilter();
		html += ' <a href="javascript:void(0)" onclick="javascript:consultListJs.clearSearch()">清除</a>';
		$('#searchFilter').html(html);
		
	}
	function postSearch()
	{
		var str = "";
		var consult_from = $('#consult_from').datebox('getValue');
		if(typeof(consult_from) == 'undefined')
			consult_from = '';
		
		var consult_to = $('#consult_to').datebox('getValue');
		if(typeof(consult_to) == 'undefined')
			consult_to = '';
		
		str = appendURL(str,"items="+consult_items_search);
		str = appendURL(str,"employee="+consult_employee_search);
		str = appendURL(str,"from="+consult_from);
		str = appendURL(str,"to="+consult_to);
		str = appendURL(str,'start='+start+'&size='+countPerPage);
		$.ajax({
            type: "post",
            url: "dispatch/customer/opportunity/consult/all",
            dataType:"json",
            data: str,
            cache: false,
            success: function (result) {
            	
            	consultListJs.loadConsultData(result);
            }
		});
	}
	function appendURL(url,str1)
	{
		if(typeof(str1) == 'undefined')return url;
		if(url == "" || typeof(url) == 'undefined')url = str1;
		else url+="&"+str1;

		return url;
	}
}( window.consultListJs = window.consultListJs || {}, jQuery ));

$(function(){
	consultListJs.init();
});    	

    </script>
    <style type="text/css">
        #fm{
            margin:0;
            padding:10px 30px;
        }
        .ftitle{
            font-size:14px;
            font-weight:bold;
            padding:5px 0;
            margin-bottom:10px;
            border-bottom:1px solid #ccc;
        }
        .fitem{
            margin-bottom:5px;
        }
        .fitem label{
            display:inline-block;
            width:80px;
        }
        .toolbar_pic {
 			width: 70px;
  			height: 30px;
  			overflow: hidden;
  			display: inline-block;
  			vertical-align: top;
  			cursor: pointer;
  			opacity: 0.6;
  			filter: alpha(opacity=60);
		}
		.toolbar_text {
 			width: 70px;
 			height:100%;
  			overflow: hidden;
  			display: inline-block;
  			vertical-align: middle;
		}
    </style>